package com.mystnihon.freeremote.db.table;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.mystnihon.freeremote.db.DBProvider;

public class ProgramTable extends GenericTable {

    public static final String TABLE_NAME = "program";
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.freeremote." + TABLE_NAME;

    /**
     * The MIME type of a {@link #CONTENT_URI} sub-directory of a single note.
     */
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.freeremote." + TABLE_NAME;

    public static final Uri CONTENT_URI = Uri.parse("content://" + DBProvider.AUTHORITY + "/" + TABLE_NAME);

    public static final String COLUMN_CHANNEL_ID = "channel_id";
    public static final String COLUMN_PROGRAM_ID = "program_id";
    public static final String COLUMN_TITLE = "title";
    public static final String COLUMN_RESUME = "resume";
    public static final String COLUMN_DESCRIPTION = "description";
    public static final String COLUMN_DURATION = "duration";
    public static final String COLUMN_STARTTIME = "starttime";
    public static final String COLUMN_ENDTIME = "endtime";
    public static final String COLUMN_GENRE = "genre";

    public int mChannelId;
    public long mProgramId;
    public String mTitle;
    public String mResume;
    public String mDescription;
    public String mDuration;
    public long mStartTime;
    public long mEndTime;
    public int mGenre;

    // column position in projection
    public static final int CONTENT_PROGRAM_ID_COLUMN = 1;
    public static final int CONTENT_CHANNEL_ID_COLUMN = 2;
    public static final int CONTENT_TITLE_COLUMN = 3;
    public static final int CONTENT_RESUME_COLUMN = 4;
    public static final int CONTENT_DESCRIPTION_COLUMN = 5;
    public static final int CONTENT_DURATION_COLUMN = 6;
    public static final int CONTENT_STARTTIME = 7;
    public static final int CONTENT_ENDTIME = 8;
    public static final int CONTENT_GENRE = 9;
    private static final String TAG = ProgramTable.class.getSimpleName();

    public static String[] CONTENT_PROJECTION = new String[] {
            _ID, COLUMN_PROGRAM_ID, COLUMN_CHANNEL_ID, COLUMN_TITLE, COLUMN_RESUME, COLUMN_DESCRIPTION, COLUMN_DURATION, COLUMN_STARTTIME, COLUMN_ENDTIME, COLUMN_GENRE
    };

    public static String[] CONTENT_PROJECTION_SMALL = new String[] {
            _ID, COLUMN_PROGRAM_ID
    };

    public static String getCreateStatement() {
        final StringBuffer str = new StringBuffer("CREATE TABLE ");
        str.append(TABLE_NAME);
        str.append(" (");
        str.append(_ID);
        str.append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
        str.append(COLUMN_PROGRAM_ID);
        str.append(" INTEGER, ");
        str.append(COLUMN_CHANNEL_ID);
        str.append(" INTEGER, ");
        str.append(COLUMN_TITLE);
        str.append(" TEXT, ");
        str.append(COLUMN_RESUME);
        str.append(" TEXT, ");
        str.append(COLUMN_DESCRIPTION);
        str.append(" TEXT, ");
        str.append(COLUMN_DURATION);
        str.append(" TEXT, ");
        str.append(COLUMN_STARTTIME);
        str.append(" INTEGER, ");
        str.append(COLUMN_ENDTIME);
        str.append(" INTEGER, ");
        str.append(COLUMN_GENRE);
        str.append(" INTEGER); ");

        Log.d(TAG, "create court table: " + str.toString());
        return str.toString();
    }

    public ContentValues getContentValues() {
        final ContentValues content = new ContentValues();
        content.put(COLUMN_CHANNEL_ID, mChannelId);
        content.put(COLUMN_PROGRAM_ID, mProgramId);
        content.put(COLUMN_TITLE, mTitle);
        content.put(COLUMN_RESUME, mResume);
        content.put(COLUMN_DESCRIPTION, mDescription);
        content.put(COLUMN_DURATION, mDuration);
        content.put(COLUMN_STARTTIME, mStartTime);
        content.put(COLUMN_ENDTIME, mEndTime);
        content.put(COLUMN_GENRE, mGenre);
        return content;
    }

    public static ProgramTable inflateObject(Cursor c) {
        final ProgramTable program = new ProgramTable();
        program.mChannelId = c.getInt(CONTENT_CHANNEL_ID_COLUMN);
        program.mProgramId = c.getLong(CONTENT_PROGRAM_ID_COLUMN);
        program.mTitle = c.getString(CONTENT_TITLE_COLUMN);
        program.mResume = c.getString(CONTENT_RESUME_COLUMN);
        program.mDescription = c.getString(CONTENT_DESCRIPTION_COLUMN);
        program.mDuration = c.getString(CONTENT_DURATION_COLUMN);
        program.mStartTime = c.getLong(CONTENT_STARTTIME);
        program.mEndTime = c.getLong(CONTENT_ENDTIME);
        program.mGenre = c.getInt(CONTENT_GENRE);
        return program;
    }

    public ProgramTable() {

    }
}
